{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2766b145-692c-4d96-870e-889d288d48bb",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import os\n",
    "import shutil\n",
    "\n",
    "# 读取csv文件\n",
    "path = '../dataset/TomatoPestAndDiseaseDataset/valid/'\n",
    "df = pd.read_csv(path + '_classes.csv')\n",
    "\n",
    "# 遍历每一行\n",
    "for index, row in df.iterrows():\n",
    "    # 遍历除了第一列以外的每一列\n",
    "    for col in df.columns[1:]:\n",
    "        # 如果该位置是1\n",
    "        if row[col] == 1:\n",
    "            # 获取该列名\n",
    "            folder_name = col\n",
    "            # 创建文件夹\n",
    "            if not os.path.exists(path + folder_name):\n",
    "                os.makedirs(path + folder_name)\n",
    "            # 将该行第一列的文件存入该文件夹中\n",
    "            if not os.path.exists(path + folder_name + '/' + row[df.columns[0]]):\n",
    "                shutil.move(path + row[df.columns[0]], path + folder_name + '/' + row[df.columns[0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc516da2-d5ab-4b31-9cdc-83ad74f4d52a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
